package br.com.nooclix.salon.controller;

import br.com.nooclix.salon.application.ServiceApplication;
import br.com.nooclix.salon.controller.util.ResponseData;
import br.com.nooclix.salon.domain.service.Service;
import br.com.nooclix.salon.infrastructure.util.ApplicationContextRegister;

public class ServiceController {

	private ServiceApplication serviceApplication = ApplicationContextRegister
			.getBean("serviceApplication", ServiceApplication.class);

	public ResponseData persist(Service service) {
		ResponseData responseData = new ResponseData();

		try {

			// configurando para ser NULL por causa do FLEX ter padrao 0(ZERO)
			service.setCode(null);
			serviceApplication.persist(service);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}
	
	public ResponseData update(Service service) {
		ResponseData responseData = new ResponseData();

		try {

			// configurando para ser NULL por causa do FLEX ter padrao 0(ZERO)
			
			serviceApplication.update(service);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}
	
	public ResponseData remove(Service service) {
		ResponseData responseData = new ResponseData();

		try {

			// configurando para ser NULL por causa do FLEX ter padrao 0(ZERO)
			
			serviceApplication.delete(service);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}
	
	public ResponseData findByName(String name) {
		ResponseData responseData = new ResponseData();

		try {

			responseData.setItems(serviceApplication.findByName(name));
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

}